package cn.iocoder.yudao.module.promotion.controller.admin.shortlinkgeneratee;

import cn.iocoder.yudao.module.promotion.controller.admin.shortlinkgeneratee.vo.*;
import cn.iocoder.yudao.module.promotion.convert.shortlinkgeneratee.ShortLinkGenerateeConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.shortlinkgeneratee.ShortLinkGenerateeDO;
import cn.iocoder.yudao.module.promotion.service.shortlinkgeneratee.ShortLinkGenerateeService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

@Tag(name = "管理后台 - 短链接生成")
@RestController
@RequestMapping("/promotion/short-link-generatee")
@Validated
public class ShortLinkGenerateeController {

    @Resource
    private ShortLinkGenerateeService shortLinkGenerateeService;

    @PostMapping("/create")
    @Operation(summary = "创建短链接生成")
    @PreAuthorize("@ss.hasPermission('zfkj:short-link-generatee:create')")
    public CommonResult<Long> createShortLinkGeneratee(@Valid @RequestBody ShortLinkGenerateeCreateReqVO createReqVO) {
        return success(shortLinkGenerateeService.createShortLinkGeneratee(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新短链接生成")
    @PreAuthorize("@ss.hasPermission('zfkj:short-link-generatee:update')")
    public CommonResult<Boolean> updateShortLinkGeneratee(@Valid @RequestBody ShortLinkGenerateeUpdateReqVO updateReqVO) {
        shortLinkGenerateeService.updateShortLinkGeneratee(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除短链接生成")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zfkj:short-link-generatee:delete')")
    public CommonResult<Boolean> deleteShortLinkGeneratee(@RequestParam("id") Long id) {
        shortLinkGenerateeService.deleteShortLinkGeneratee(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得短链接生成")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zfkj:short-link-generatee:query')")
    public CommonResult<ShortLinkGenerateeRespVO> getShortLinkGeneratee(@RequestParam("id") Long id) {
        ShortLinkGenerateeDO shortLinkGeneratee = shortLinkGenerateeService.getShortLinkGeneratee(id);
        return success(ShortLinkGenerateeConvert.INSTANCE.convert(shortLinkGeneratee));
    }

    @GetMapping("/list")
    @Operation(summary = "获得短链接生成列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('zfkj:short-link-generatee:query')")
    public CommonResult<List<ShortLinkGenerateeRespVO>> getShortLinkGenerateeList(@RequestParam("ids") Collection<Long> ids) {
        List<ShortLinkGenerateeDO> list = shortLinkGenerateeService.getShortLinkGenerateeList(ids);
        return success(ShortLinkGenerateeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得短链接生成分页")
    @PreAuthorize("@ss.hasPermission('zfkj:short-link-generatee:query')")
    public CommonResult<PageResult<ShortLinkGenerateeRespVO>> getShortLinkGenerateePage(@Valid ShortLinkGenerateePageReqVO pageVO) {
        PageResult<ShortLinkGenerateeDO> pageResult = shortLinkGenerateeService.getShortLinkGenerateePage(pageVO);
        return success(ShortLinkGenerateeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/shortList")
    @Operation(summary = "获得短链接")
    @PreAuthorize("@ss.hasPermission('zfkj:short-link-generatee:query')")
    public CommonResult<List<ShortLinkGenerateeRespVO>> getShortList(@Valid ShortLinkGenerateePageReqVO pageVO) {
        List<ShortLinkGenerateeDO> pageResult = shortLinkGenerateeService.getShortLinkGeneratee(pageVO);
        return success(ShortLinkGenerateeConvert.INSTANCE.convertList(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出短链接生成 Excel")
    @PreAuthorize("@ss.hasPermission('zfkj:short-link-generatee:export')")
    public void exportShortLinkGenerateeExcel(@Valid ShortLinkGenerateeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShortLinkGenerateeDO> list = shortLinkGenerateeService.getShortLinkGenerateeList(exportReqVO);
        // 导出 Excel
        List<ShortLinkGenerateeExcelVO> datas = ShortLinkGenerateeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "短链接生成.xls", "数据", ShortLinkGenerateeExcelVO.class, datas);
    }

    @PutMapping("/close")
    @Operation(summary = "关闭短链接")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zfkj:domain-manage:close')")
    public CommonResult<Boolean> closeShortLinkGeneratee(@RequestParam("id") Long id) {
        shortLinkGenerateeService.closeShortLinkGeneratee(id);
        return success(true);
    }
}
